<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 退出
 *
 * @author      Eden
 * @datetime    2016/12/1 19:39
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Logout_Model extends API_Model
{

    private $params = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主函数
     * @param array $params
     */
    public function run($params = array())
    {
        $this->params = $params;
        $send['state'] = false;
        do {

            if(!$this->checkParams()) {
                $send['error'] = $this->getError();
                break;
            }

            try {
                $deleteDB = clone($this->db);
                $query = $this->db->get_where('user_mobile_login', array('deviceId' => $params['deviceId']));
                if ($result = $query->row_array()) {
                    $deleteDB->where('deviceId', $params['deviceId']);
                    $deleteDB->delete('user_mobile_login');
                    if ($deleteDB->affected_rows()) {
                        $send['state'] = true;
                        $send['data'] = array(
                            'msg' => '退出成功'
                        );
                        break;
                    }
                }
                $send['error'] = '退出遇到错误';
            } catch (PDOException $e) {
                $send['error'] = '退出失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 检查参数
     * @return bool
     */
    private function checkParams()
    {
        if (empty($this->params['userId']) || empty($this->params['deviceId'])) {
            $this->setError('参数错误');
            return false;
        }

        return true;
    }

}
